﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;

namespace CitiSurface
{

    /// <summary>
    /// Interaction logic for VideoBarControl.xaml
    /// </summary>
    public partial class VideoBarControl : UserControl, IBarControl
    {

        public VideoBarControl()
        {
            InitializeComponent();
        }

#region Touch Event
        public static readonly RoutedEvent ClickElementEvent =
    EventManager.RegisterRoutedEvent("ClickElement", RoutingStrategy.Bubble,
                                                       typeof(RoutedEventHandler), typeof(VideoBarControl));

        public event RoutedEventHandler ClickElement
        {
            add { AddHandler(ClickElementEvent, value); }
            remove { RemoveHandler(ClickElementEvent, value); }
        }

        public static readonly DependencyProperty SelectedItemProperty =
    DependencyProperty.Register("SelectedItem", typeof(object), typeof(VideoBarControl));

        public object SelectedItem
        {
            get { return (object)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }

        private TouchPoint _startPoint;
        private DateTime _clickStartTime;

        private void ElementFlowTouchDown(object sender, TouchEventArgs e)
        {
            _startPoint = e.GetTouchPoint(elementFlow);

            //check if the startpoint is within the region
            if ((e.TouchDevice.DirectlyOver as ModelUIElement3D) != null)
            {
                _clickStartTime = DateTime.Now;
            }
        }

        private void ElementFlowTouchMove(object sender, TouchEventArgs e)
        {
            if (_startPoint == null)
                return;

            var currPoint = e.GetTouchPoint(elementFlow);
            var xDiff = currPoint.Position.X - _startPoint.Position.X;
            if(xDiff > 100) //swipe right
            {
                elementFlow.SelectedIndex = elementFlow.SelectedIndex - 1;
                _startPoint = currPoint;
                _clickStartTime = DateTime.MinValue;//cancel the click event
            }
            if(xDiff < -100) //swipe left
            {
                elementFlow.SelectedIndex = elementFlow.SelectedIndex + 1;
                _startPoint = currPoint;
                _clickStartTime = DateTime.MinValue;//cancel the click event
            }
        }

        private void ElementFlowTouchUp(object sender, TouchEventArgs e)
        {
            _startPoint = null;
            if ((e.TouchDevice.DirectlyOver as ModelUIElement3D) != null)
            {
                lock(this)
                {
                    var currTime = DateTime.Now;
                    var diff = currTime - _clickStartTime;
                    if(diff.TotalMilliseconds < 200)
                    {
                        _clickStartTime = DateTime.MinValue;
                        RaiseEvent(new RoutedEventArgs(ClickElementEvent, this));
                    }
                }
            }
        }

        private void ElementFlowTouchLeave(object sender, TouchEventArgs e)
        {
            _startPoint = null;
        }

        private void ElementFlowSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.SelectedItem = elementFlow.SelectedItem;
        }
#endregion
    }
}
